package com.qf.java2107.yb.hotel.controller.front;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.qf.java2107.yb.hotel.common.ResultVO;
import com.qf.java2107.yb.hotel.constant.MethodConstant;
import com.qf.java2107.yb.hotel.controller.BaseServlet;
import com.qf.java2107.yb.hotel.entity.BeanFactory;
import com.qf.java2107.yb.hotel.pojo.Food;
import com.qf.java2107.yb.hotel.service.FoodService;
import com.qf.java2107.yb.hotel.service.FrontService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yb
 * @version 1.0
 * @date 2021-10-28
 **/
@WebServlet("/front")
public class FrontController extends BaseServlet {

    private FrontService frontService = (FrontService) BeanFactory.getBean("frontService");
    List<Food> list = new ArrayList<>();

    public String showFoodListPage(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String typeIdStr = request.getParameter("typeId");
        String foodName = request.getParameter("foodName");

        if (StringUtils.isEmpty(typeIdStr)) {
            typeIdStr = "0";
        }
        Integer typeId = Integer.parseInt(typeIdStr);

        //页码
        String currentPage = request.getParameter("currentPage");
        //页大小
        String pageSize = request.getParameter("pageSize");
        //分页显示菜品数据
        ResultVO vo = frontService.showFoodListPage(currentPage,pageSize,typeId, foodName);

        if(vo.getSuccess()) {
            //把查询条件回传
            request.setAttribute("searchTypeId", typeId);
            request.setAttribute("searchFoodName", foodName);
            request.setAttribute("pageBean", vo.getData());
            //转发
            return MethodConstant.FORWARD_PREFIX + getServletContext().getAttribute("frontDetailFolder") + "/caidan.jsp";
        }
        //显示菜单的数据放入到session中
        return (String) vo.getData();
    }

    /**
     * 把查到的菜系列表数据放进session中
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String loadFoodTypeList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //把菜系列表数据放入到session中
        ResultVO vo = frontService.loadFoodTypeList();
        if(vo.getSuccess()) {
            getServletContext().setAttribute("typeList", vo.getData());
            //request.getSession().setAttribute("typeList", vo.getData());
        }
        return JSON.toJSONString(vo);
    }


    /**
     * 菜详情
     * @param request
     * @param response
     * @return
     */
    public String findById(HttpServletRequest request, HttpServletResponse response){
        String foodId = request.getParameter("foodId");
        ResultVO vo = frontService.findFoodById(Integer.valueOf(foodId));

        if(vo.getSuccess()){
            request.setAttribute("food",vo.getData());
            return MethodConstant.FORWARD_PREFIX + getServletContext().getAttribute("frontDetailFolder") + "/caixiangxi.jsp";
        }
        return vo.getMessage();
    }

    /**
     * 点击加入购物车，把food放进Session中
     * @param request
     * @param response
     * @return
     */
    public String inputSession(HttpServletRequest request, HttpServletResponse response){
        String foodId = request.getParameter("foodId");
        ResultVO vo = frontService.findFoodById(Integer.valueOf(foodId));
        Food data = (Food) vo.getData();

        list.add(data);

        if(vo.getSuccess()){
            request.getSession().setAttribute("foodList", list);
            return MethodConstant.FORWARD_PREFIX + getServletContext().getAttribute("frontDetailFolder") + "/clientCart.jsp";
        }
        return vo.getMessage();
    }
}
